<template>
  <transition name="slide">
   <music-list :songs="songs" :bg-image="bgImage" :title="title"></music-list>
  </transition>
</template>

<script type="text/ecmascript-6">
  import {mapGetters} from 'vuex'
  import {ERR_OK} from "../../api/config";
  import {getSingerDetail} from "../../api/singer";
  import {createSong,isValidMusic,processSongsUrl} from "../../common/js/song";
  import MusicList from "../music-list/music-list";
  import {getMusic} from "../../api/singer";

  export default {
    computed: {
      title() {
        return this.singer.name
      },
      bgImage() {
        return this.singer.avatar
      },
      ...mapGetters([
        //singer组件里面的singer值
        'singer'
      ])
    },
    data() {
      return {
        songs: []
      }
    },
    created() {

      this._getDetail()
    },
    methods: {
      _getDetail() {
        //刷新时自动跳到歌手页面
        if (!this.singer.id) {
          this.$router.push('/singer')
          return
        }
        //歌手歌曲
        getSingerDetail(this.singer.id).then((res) => {
          if (res.code === ERR_OK) {
            //某个歌手整个的歌曲
            this.songs = this._normalizeSongs(res.data.list)
          }
        })
      },
      //获取每个歌手的歌曲信息
      _normalizeSongs(list) {
        // console.log(list)
        let ret = []
        list.forEach((item) => {
          let {musicData} = item
          if (isValidMusic(musicData)) {
            ret.push(createSong(musicData))
          }
        })
        return ret
      }
    },
    components: {
      MusicList
    }
  }
</script>


<style lang="stylus" rel="stylesheet/stylus">
  @import '../../common/stylus/variable.styl';
  .slide-enter-active,.slide-leave-active
    transition all 0.3s
  .slide-enter,.slide-leave-to
    transform translate3d(100%,0,0)
</style>
